package DAO;

import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Model.Exame;
import Model.TipoExame;
import Utils.Conexao;

public class ExameDAO {
	public static void Insert(Exame exame) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_exame(?, ?, ?, ?, ?, ?, ?);";			
		CallableStatement call =ExameDAO.setObjectPraparedStatement(exame, mysqlCall, con);
		call.execute();
		
		call.close();
		con.close();		
	}
	
	public static void Update(Exame exame) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_exame(?, ?, ?, ?, ?, ?, ?, ?)";		
		CallableStatement call =ExameDAO.setObjectPraparedStatement(exame, mysqlCall, con) ;
		call.execute();
		
		con.close();
		call.close();
	}
	
	public static ArrayList<Exame> GetArrayListExames()
	{
		ArrayList<Exame> arrExames = new ArrayList<Exame>();
		try {			
			Connection con = Conexao.getConection();
			String mysqlCall = "call spc_select_exames()";			
			CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
			ResultSet res = call.executeQuery();
			
			while(res.next())
			{
				Exame exame = ExameDAO.setInstance(res);
				arrExames.add(exame);
			}
			
			call.close();
			con.close();			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrExames;
	}	
	
	public static void InativarExame(int id_exame) throws SQLException
	{
		Connection con = Conexao.getConection();
		String mysqlCall = "call spc_inativar_exame(?)";
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, id_exame);
		call.execute();
		
		call.close();
		con.close();
	}
	
	public static Exame LoadObjet(int id) throws SQLException
	{		
		Exame exame = null;
		Connection con = Conexao.getConection();
		String mysqlCall = "call spc_select_exame_por_id(?)";
		
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, id);
		ResultSet res = call.executeQuery();
		
		if(res.next())
		{
			exame = ExameDAO.setInstance(res);			
		}
		
		con.close();
		call.close();		
		return exame;
	}
	
	
	private static CallableStatement setObjectPraparedStatement(Exame exame, String mysqlCall, Connection con ) throws SQLException
	{				
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setString(1, exame.getDescricao());
		call.setString(2, exame.getResultado());
		call.setDate(3, exame.getDataExame());
		call.setInt(4, exame.getIdTipoExame());		
		call.setInt(5, exame.getIdLaboratorio());
		call.setInt(6, exame.getIdPaciente());
		call.setInt(7, exame.getIdUsuario());
		
		if(exame.getIdExame() > 0)
			call.setInt(8, exame.getIdExame());

		return call; 		
	}
	
	public static Exame setInstance(ResultSet res) throws SQLException
	{
		Exame exame = new Exame();	
		
		exame.setIdExame(res.getInt("id_exame"));
		exame.setDescricao(res.getString("descricao"));
		exame.setResultado(res.getString("resultado"));
		exame.setDataExame(res.getDate("data_exame"));
		exame.setIdPaciente(res.getInt("id_paciente"));
		exame.setIdUsuario(res.getInt("id_usuario"));
		exame.setIdLaboratorio(res.getInt("id_laboratorio"));
		exame.setIdTipoExame(res.getInt("id_tipo_exame"));
		exame.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
		exame.setUsuario(UsuarioDAO.LoadObject(res.getInt("id_usuario")));
		
		return exame;
	}
}
